package com.sc.serialize;

import java.io.*;

/**
 * Created by Shen Chao on 2018-06-16-016.
 */
public class SerializableTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        serializePig();
        Pig pig = deserializePig();
        System.out.println(pig.toString());
    }
    /**
     * 序列化
     */
    private static void serializePig() throws IOException {
        Pig pig = new Pig();
        pig.setName("pig3");
        pig.setColor("black");
        pig.setCar("123456");
        //使用输出流，将对象存储到d:/pig.txt 完成序列化
        ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream(new File("d:/pig.txt")));
        oos.writeObject(pig);
        System.out.println("序列化成功");
        oos.close();
    }

    /**
     * 序列化
     */
    private static Pig deserializePig() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream(new File("d:/pig.txt")));
        Pig pig = (Pig) ois.readObject();
        return pig;
    }
}
